package com.wrupple.muba.catalogs.client.activity.process.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.activity.process.FilterSelectionProcess;
import com.wrupple.muba.catalogs.client.activity.process.impl.ForeignKeyFilterSelectionProcessImpl.FilterSelection;
import com.wrupple.muba.catalogs.client.activity.process.task.FilterOptionSelectionState;
import com.wrupple.muba.catalogs.client.activity.process.task.GenericSelectionState;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.catalogs.domain.CatalogProcessDescriptor;
import com.wrupple.muba.common.domain.FilterCriteria;
import com.wrupple.muba.common.domain.FilterData;
import com.wrupple.muba.common.shared.SequentialProcess;
import com.wrupple.muba.common.shared.State;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class ForeignKeyFilterSelectionProcessImpl extends SequentialProcess<List<FieldDescriptor>,FilterSelection>
		implements FilterSelectionProcess {

	public static class FilterSelection {
		String fieldid;
		//i think this is cute :-3 
		List<FilterCriteria> criters;
		public String getFieldid() {
			return fieldid;
		}
		public void setFieldid(String fieldid) {
			this.fieldid = fieldid;
		}
		public List<FilterCriteria> getCriters() {
			return criters;
		}
		public void setCriters(List<FilterCriteria> criters) {
			this.criters = criters;
		}
		
		
	}
	
	static class CatalogEntryToFilterSelection implements State<List<CatalogEntry>,FilterSelection>{

		private FilterOptionSelectionState option;

		public CatalogEntryToFilterSelection(FilterOptionSelectionState option) {
			this.option=option;
		}

		@Override
		public void start(List<CatalogEntry> parameter,
				StateTransition<FilterSelection> onDone, EventBus bus) {
			FilterSelection regreso = new FilterSelection();
			List<FilterCriteria> criters = new ArrayList<FilterCriteria>();
			FilterCriteria filter;
			for(CatalogEntry entry : parameter){
				filter = new FilterCriteria();
				filter.setOperator(FilterData.EQUALS);
				filter.setValue(entry.getIdAsString());
				criters.add(filter);
			}
			FieldDescriptor field = option.getSelectedFieldDescriptor();
			regreso.setFieldid(field.getId());
			regreso.setCriters(criters);
			onDone.setResultAndFinish(regreso );
		}
		
	}
	
	static class SelectionToPlace implements State<FieldDescriptor,CatalogProcessDescriptor>{

		@Override
		public void start(FieldDescriptor parameter,
				StateTransition<CatalogProcessDescriptor> onDone, EventBus bus) {
			assert parameter.isKey() : "Filtering non key fields is not supported (only key fields are supported)";
			String catalog = parameter.getForeignCatalog();
			CatalogProcessDescriptor result = new CatalogProcessDescriptor(new FilterData(), catalog, null);
			onDone.setResultAndFinish(result );
		}
		
	}
	
	
	@Inject
	public ForeignKeyFilterSelectionProcessImpl(FilterOptionSelectionState option, GenericSelectionState selection) {
		super();
		add(option);
		add(new SelectionToPlace());
		add(selection);
		add(new CatalogEntryToFilterSelection(option));
	}



}
